<?php
class Generate_Model_Generate extends Zend_Db_Table_Abstract
{
	protected $_name = 'lib_search';
    public function __construct($config = array()) {
       $this->_db = Zend_Db_Table::getDefaultAdapter();
       $this->_pdo = new Zend_Db_Table($this->_name);
       parent::__construct($config);
    }    
	
    public function createModule($Params){
    $moduleName         = $Params['moduleName'];
    $moduleCreate       = $Params['moduleCreate'];
    $bootstrapName      = $Params['bootstrapName'];
    // tạo đường dẫn các thư mục
    $module_path        = APPLICATION_PATH."/modules/".strtolower($moduleCreate);
    $controller_path    = $module_path."/controllers";
    $model_path         = $module_path."/models";
    $view_path          = $module_path."/views";
    $filters_path       = $module_path."/views/filters";
    $helpers_path       = $module_path."/views/helpers";
    $scripts_path       = $module_path."/views/scripts";
    
    // tạo thư mục module
    mkdir($module_path, 0700);
    mkdir($controller_path, 0700);
    mkdir($model_path, 0700);
    mkdir($view_path, 0700);
    mkdir($filters_path, 0700);
    mkdir($helpers_path, 0700);
    mkdir($scripts_path, 0700);
    
    // tạo bootstrap
    $file = new Zend_CodeGenerator_Php_File(array(
            'classes' => array(
                new Zend_CodeGenerator_Php_Class(array(
                    'name'    => $bootstrapName,
                    'methods' => array(
                        new Zend_CodeGenerator_Php_Method(array(
                            'name' => 'hello',
                            'body' => 'echo \'Hello world!\';',
                        )),
                    ),
                )),
            )
        ));
         
        // Configuring after instantiation
        $docblock = new Zend_CodeGenerator_Php_Docblock();
        $docblock->setShortDescription('Auto Code generation');
        $docblock->setLongDescription('Một sản phẩm sinh mã tự động');
        $tag = array(
            array(
                'name'        => 'version',
                'description' => '$Rev:$',
            ),
            array(
                'name'        => 'license',
                'description' => 'Baotq Daniel Chivas',
            )
        );
        $docblock->setTags($tag);
         
        $class = new Zend_CodeGenerator_Php_Class();
        $class->setName($bootstrapName)
              ->setExtendedClass('Zend_Application_Module_Bootstrap')
              ->setDocblock($docblock);
         
        $file = new Zend_CodeGenerator_Php_File();
        $file->setClass($class);

        // or write it to a file:
        file_put_contents($module_path.'/Bootstrap.php', $file->generate());
    }
    public function createController($Params){
        if($Params['controllerName'] != ''){
        $moduleName         = $Params['moduleName'];
        $moduleCreate       = $Params['moduleCreate'];
        $bootstrapName      = $Params['bootstrapName'];
        $controllerName     = $Params['controllerName'];
        $modelName          = $Params['modelName'];
        $controllerDescription = $Params['controllerDescription'];
        $singleNameController  = $Params['singleNameController'];
        
        $controllerFileName = split('_',$controllerName);
        $module_path        = APPLICATION_PATH."/modules/".$moduleCreate;
        $controller_path    = $module_path."/controllers";
        $view_path    = $module_path."/views/scripts/".$singleNameController;

        if($controllerDescription == ""){
            $controllerDescription = $controllerFileName[1];
        }
        // tạo thư mục view cuả controller
        mkdir($view_path, 0700);
        // tạo file Controller
        $file = new Zend_CodeGenerator_Php_File(array(
                'classes' => array(
                    new Zend_CodeGenerator_Php_Class(array(
                        'name'    => $controllerName,
                        'methods' => array(
                            new Zend_CodeGenerator_Php_Method(array(
                                'name' => 'hello',
                                'body' => 'echo \'Hello world!\';',
                            )),
                        ),
                    )),
                )
            ));
             
            // Configuring after instantiation
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $docblock->setShortDescription($controllerDescription);
            $docblock->setLongDescription('Được sinh mã tự động');
            $tag = array(
                array(
                    'name'        => 'version',
                    'description' => '$Rev:$',
                ),
                array(
                    'name'        => 'license',
                    'description' => 'Baotq Daniel Chivas',
                )
            );
            $docblock->setTags($tag);
            
            $modelClassName = preg_replace('/_DbTable/', '', get_class($this));
            $class = new Zend_CodeGenerator_Php_Class();
            $class->setName($controllerName)
                  ->setExtendedClass('Zend_Controller_Action')
                  ->setDocblock($docblock);              
            
            if(isset($Params['isInit']))
            {
                $body = '';
                if(isset($Params['isGetRequest'])){
                    $body = '$this->params = $this->getRequest()->getParams();'."\n";
                }
                if($Params['layoutName'] != '' && $Params['templateName'] != ''){
                    $body .= 'System_Template::using("'.$Params['layoutName'].'","'.$Params['templateName'].'");'."\n";
                }
                if($Params['isCreateModel'] == "1" && $modelName != ''){
                    $body .= '$this->model = new '.$modelName.'();'."\n";
                }
                $body .= 'parent::init();';
                $class->setMethod(array(
                        'name' => 'init',
                        'body' => $body
                        )
                );
            }
            if(count($Params['actionName']) > 0)
            {
                foreach($Params['actionName'] as $item)
                {
                    if($item != "")
                    {
                       $class->setMethod($this->createAction($item,''));
                    }
                }
            }
            $file = new Zend_CodeGenerator_Php_File();
            $file->setClass($class);
             
            // or write it to a file:
            file_put_contents($controller_path.'/'.$controllerFileName[1].'.php', $file->generate());
        }
    }
    
	public function createTableClass($Params){
        if($Params['TABLE_NAME'] != "" && $Params['CLASS_NAME'] != ""){
            $system_Library_Path =  dirname(dirname(dirname(dirname(dirname(__FILE__))))). '/library/System/Table';
            $class_Name = $Params['CLASS_NAME'];
            $table_Name = $Params['TABLE_NAME'];
            $primary_Key = $Params['PRIMARY_KEY'];
            $fileName = explode('_',$Params['CLASS_NAME']);
            $file = new Zend_CodeGenerator_Php_File(array(
                    'classes' => array(
                        new Zend_CodeGenerator_Php_Class(array(
                            'name'    => $class_Name,
                            'methods' => array(
                                new Zend_CodeGenerator_Php_Method(array(
                                    'name' => 'hello',
                                    'body' => 'echo \'Hello world!\';',
                                )),
                            ),
                        )),
                    )
                ));
                 
            // Configuring after instantiation
            $docblock = new Zend_CodeGenerator_Php_Docblock();
            $docblock->setShortDescription($fileName[2]);
            $docblock->setLongDescription('Được sinh mã tự động');
            $tag = array(
                array(
                    'name'        => 'version',
                    'description' => '0.1'
                ),
                array(
                    'name'        => 'license',
                    'description' => 'Baotq Daniel Chivas'
                ),                
                array(
                    'name'        => 'date',
                    'description' => date("d-m-Y")
                )
            );
            $docblock->setTags($tag);
            
            $class = new Zend_CodeGenerator_Php_Class();
            $class->setName($class_Name)
                  ->setExtendedClass('Zend_Db_Table_Abstract')
                  ->setDocblock($docblock);
                  
            $class->setProperties($this->createProperty(Array ( 0 => '_name' ,1 => 'protected' ) ,$table_Name));
            $class->setProperties($this->createProperty(Array ( 0 => '_primary' ,1 => 'protected' ),$primary_Key));
            
            $file = new Zend_CodeGenerator_Php_File();
            $file->setClass($class);
            //echo '<h1>'.$system_Library_Path.'/'.$fileName[2].'.php'.'</h1>';exit;
            file_put_contents($system_Library_Path.'/'.$fileName[2].'.php', $file->generate());
        }
    }
	public function createModel($Params){
        if($Params['modelSingleName'] != ""){
            $moduleName         = $Params['moduleName'];
            $moduleCreate       = $Params['moduleCreate'];
            $modelSingleName    = $Params['modelSingleName'];
            $modelName          = $Params['modelName'];
            // tạo thư mục module
            $module_path        = APPLICATION_PATH."/modules/".$moduleCreate;
            $model_path         = $module_path."/models";

            
            // tạo file model
            $file = new Zend_CodeGenerator_Php_File(array(
                    'classes' => array(
                        new Zend_CodeGenerator_Php_Class(array(
                            'name'    => $modelName,
                            'methods' => array(
                                new Zend_CodeGenerator_Php_Method(array(
                                    'name' => 'hello',
                                    'body' => 'echo \'Hello world!\';',
                                )),
                            ),
                        )),
                    )
                ));
                 
                // Configuring after instantiation
                $docblock = new Zend_CodeGenerator_Php_Docblock();
                $docblock->setShortDescription($modelSingleName);
                $docblock->setLongDescription('Được sinh mã tự động');
                $tag = array(
                    array(
                        'name'        => 'version',
                        'description' => '$Rev:$',
                    ),
                    array(
                        'name'        => 'license',
                        'description' => 'Baotq Daniel Chivas',
                    )
                );
                $docblock->setTags($tag);
                
                $modelClassName = preg_replace('/_DbTable/', '', get_class($this));
                $class = new Zend_CodeGenerator_Php_Class();
                $class->setName($modelName)
                      ->setExtendedClass('Zend_Db_Table_Abstract')
                      ->setDocblock($docblock);
                      
               
                
                //$method = new Zend_CodeGenerator_Php_Method(); 
                if(isset($Params['isConstruct']))
                {
                    if(isset($Params['isAdapterCSDL'])){
                        $body = '$this->_db = Zend_Db_Table::getDefaultAdapter();'."\n";
                    }
                    if($Params['pdoName'] != "" ){
                        $body .= '$this->_pdo = new Zend_Db_Table("'.$Params['pdoName'].'");'."\n";
                    }
                    $body .= 'parent::__construct($config);';
                    $class->setMethod(array(
                            'name' => '__construct',
                            'parameters' => array(
                                array('name' => 'config = array()')
                            ),
                            'body' => $body
                            )
                    );
                }
                if(count($Params['methods']) > 0)
                {
                    foreach($Params['methods'] as $item)
                    {
                        
                        if($item[0] != "")
                        {
                           $class->setMethod($this->createMethod($item,''));
                        }
                    }
                }
                if(count($Params['properties']) > 0)
                {
                    
                    foreach($Params['properties'] as $item)
                    {
                        if($item[0] != ""){
                            $class->setProperties($this->createProperty($item,''));
                        }
                    }
                }
                $file = new Zend_CodeGenerator_Php_File();
                $file->setClass($class);
                 
                // or write it to a file:
                file_put_contents($model_path.'/'.$modelSingleName.'.php', $file->generate());
            }
    }
    public function createView($Params){
        if(count($Params['viewName']) > 0){
            $moduleCreate       = $Params['moduleCreate'];
            // tạo thư mục module
            $module_path        = APPLICATION_PATH."/modules/".$moduleCreate;

            $singleNameController = $Params['singleNameController'];        
            $view_path    = $module_path."/views/scripts/".$singleNameController;
                
            $file = new Zend_CodeGenerator_Php_File();
            foreach($Params['viewName'] as $item){
                file_put_contents($view_path.'/'.$item.'.phtml', $file->generate());
            }
        }
    }
    private function createAction($name,$body){
        $arr = array(
        'name' => $name.'Action',
        'body' => $body
        );
        return $arr;
    }
    private function createMethod($item,$body){
        $visibility = $item[1];
        if(count($item) > 2)
        {
            $visibility = $item[1].' '.$item[2];
        }
        $arr = array(
        'name' => $item[0],
        'visibility'   => $visibility,
        'parameters' => array(
            array('name' => '')
            ),
        'body' => $body
        );
        return $arr;
    }
    private function createProperty($item,$body){
        $arr = array(
            array(
                'name'         => $item[0],
                'visibility'   => $item[1],
                'defaultValue' => $body
            )
        );
        return $arr;
    }
    public function setPermission($Params){
        // $arr = array(
            // 'toida' => $this->Params['toida'],
            // 'solangiahan' => $this->Params['solangiahan'],
            // 'songaygiahan' => $this->Params['songaygiahan']
        // );        
        // config::set('config.ini','muon',$arr);
        // ajax::ship('result','1');
    }
     // $class->setMethod(array(
                    // 'name' => 'setMethod',
                    // 'parameters' => array(
                        // array('name' => 'value')
                    // ),
                    // 'body' => '',
                    // 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
                        // 'shortDescription' => 'Set the test property',
                        // 'tags' => array(
                            // new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
                                // 'paramName' => 'value',
                                // 'datatype' => 'string'
                            // )),
                            // new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                                // 'datatype' => $modelClassName
                            // ))
                        // )
                    // ))
                // ))
        // ->setMethod(array(
                    // 'name' => 'getMethod',
                    // 'body' => 'return 0;',
                    // 'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
                        // 'shortDescription' => 'Get the test property',
                        // 'tags' => array(
                            // new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                                // 'datatype' => 'string'
                            // ))
                        // )
                    // ))
                // ));
}